<?php
header("Content-type: text/html; charset=utf-8"); 

   class Comment extends database_object{
        
        public $id;
        public $comment;
        public $timestamp;
        public $user_id;
        public $connection_id;
        public $likes;
        public $dislikes;
        
        protected static $table_name = "bo_comment";
        protected static $db_fields = array("id", "comment","timestamp", "user_id","connection_id","likes","dislikes");
        
        
        // Method builds a comment object
        public function build($newcomment="",$userid=0, $connection_id=0){

            if(($connection_id!=0 ))
            {
                    $comments = new Comment();
                    $comments->comment = $newcomment;
                    $comments->user_id = $userid; 
                    $comments->connection_id = $connection_id;
                    $comments->save();
 
                return true;
            }
            
            return false;
            
        }
        
        //Method returns comments on a specific company_product-id
        
        public static function find_comments_on_C_P($company_id=0, $product_id=0){
            
            if($company_id != 0){
               
                $sql = "SELECT * FROM " . self::$table_name;
                $sql .= " WHERE company_id=" .$company_id.";";

                return self::find_by_sql($sql);

            }else if
                ($product_id !=0){
                
               
                $sql = "SELECT * FROM " . self::$table_name;
                $sql .= " WHERE product_id=" .$product_id.";";

                return self::find_by_sql($sql);
            }
            else{
                
                return false;
            }
           
        }
        public function find_comments_on_Connection($id,$order){
            $b = self::sortType($order);
            
            
            
            if($id != 0){
               
                $sql = "SELECT * FROM " . self::$table_name;
                $sql .= " WHERE connection_id=" .$id. " ";
                $sql.= " {$b} ;";

                return self::find_by_sql($sql);
            }
           
        }
        
        // Methods returns the type of sort on comment querys
        public function sortType($order){
            
            if($order == 0){
                return " ORDER BY id DESC ";
            }
            if($order == 1){
                return " ORDER by likes DESC ";
            }
            if($order == 2){
                return " ORDER by dislikes DESC ";
            }
        }
        
        //Method returns comments made by a specific user by id
        public function Find_Comments_On_User($userid){
       
            
            
            if($userid != 0){
               
                $sql = "SELECT * FROM " . self::$table_name;
                $sql .= " WHERE user_id=" .$userid." ";
              
                
                return self::find_by_sql($sql);

            
                
                return false;
            }
        }
        
        //Method returns array with the 15 latest comments
        public function getRecentComments(){
            
            $sql = "SELECT * FROM `bo_comment` ORDER BY `timestamp` DESC LIMIT 15";
            
            return self::find_by_sql($sql);
        }
     

   }
        
     $comment = new Comment();
?>
